package com.meilele.biz.czh;

import com.meilele.dao.czh.FiveCommentariesMapper;
import com.meilele.dao.czh.FivePostMapper;
import com.meilele.dao.czh.FiveRevertMapper;
import com.meilele.entity.czh.FiveCommentaries;
import com.meilele.entity.czh.FivePost;
import com.meilele.entity.czh.vo.FiveRcpVo;
import com.meilele.entity.czh.vo.FiveZhuang;
import com.meilele.util.czh.Constants;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FivePostBiz {
    @Resource
    private FivePostMapper fivePostMapper;
    @Resource
    private FiveCommentariesMapper fiveCommentariesMapper;
    @Resource
    private FiveRevertMapper fiveRevertMapper;
    public List<FiveZhuang> findPosta(int pageNum){
        int firstPage=(pageNum-1)* Constants.PAGE_SIZE;
        List<FivePost> listPost=fivePostMapper.selectByPrimaryDesca(firstPage, Constants.PAGE_SIZE);
        List<FiveZhuang> list=new ArrayList<>();
        for (FivePost fivePost:listPost){
            FiveZhuang fiveZhuang=new FiveZhuang();
            int fivePostPostId=fivePost.getPostId();
            FiveCommentaries fiveCommentaries=fiveCommentariesMapper.selectByPrimaryId(fivePostPostId);
            Date date = fivePost.getCreateTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String stringDate = format.format(date);
            fivePost.setStringDate(stringDate);
         if(fiveCommentaries!=null){
             Date date1 = fiveCommentaries.getCommentariesTime();
             SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             String stringDate1 = format1.format(date1);
             fiveCommentaries.setStringTime(stringDate1);
         }
            fiveZhuang.setFivePost(fivePost);
            fiveZhuang.setFiveCommentaries(fiveCommentaries);
            list.add(fiveZhuang);
        }
        return list;
    }
    public List<FiveZhuang> findPostb(int pageNum){
        int firstPage=(pageNum-1)* Constants.PAGE_SIZE;
        List<FivePost> listPost=fivePostMapper.selectByPrimaryDescb(firstPage, Constants.PAGE_SIZE);
        List<FiveZhuang> list=new ArrayList<>();
        for (FivePost fivePost:listPost){
            FiveZhuang fiveZhuang=new FiveZhuang();
            int fivePostPostId=fivePost.getPostId();
            FiveCommentaries fiveCommentaries=fiveCommentariesMapper.selectByPrimaryId(fivePostPostId);
            Date date = fivePost.getCreateTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String stringDate = format.format(date);
            fivePost.setStringDate(stringDate);
            if(fiveCommentaries!=null) {
                Date date1 = fiveCommentaries.getCommentariesTime();
                SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String stringDate1 = format1.format(date1);
                fiveCommentaries.setStringTime(stringDate1);
            }
            fiveZhuang.setFivePost(fivePost);
            fiveZhuang.setFiveCommentaries(fiveCommentaries);
            list.add(fiveZhuang);
        }
        return list;
    }
    public List<FiveZhuang> findPostc(int pageNum){
        int firstPage=(pageNum-1)* Constants.PAGE_SIZE;
        List<FivePost> listPost=fivePostMapper.selectByPrimaryDescc(firstPage, Constants.PAGE_SIZE);
        List<FiveZhuang> list=new ArrayList<>();
        for (FivePost fivePost:listPost){
            FiveZhuang fiveZhuang=new FiveZhuang();
            int fivePostPostId=fivePost.getPostId();
            FiveCommentaries fiveCommentaries=fiveCommentariesMapper.selectByPrimaryId(fivePostPostId);
            Date date = fivePost.getCreateTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String stringDate = format.format(date);
            fivePost.setStringDate(stringDate);
            if(fiveCommentaries!=null) {
                Date date1 = fiveCommentaries.getCommentariesTime();
                SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String stringDate1 = format1.format(date1);
                fiveCommentaries.setStringTime(stringDate1);
            }
            fiveZhuang.setFivePost(fivePost);
            fiveZhuang.setFiveCommentaries(fiveCommentaries);
            list.add(fiveZhuang);
        }
        return list;
    }
    public List<FiveZhuang> findPostd(int pageNum){
        int firstPage=(pageNum-1)* Constants.PAGE_SIZE;
        List<FivePost> listPost=fivePostMapper.selectByPrimaryDescd(firstPage, Constants.PAGE_SIZE);
        List<FiveZhuang> list=new ArrayList<>();
        for (FivePost fivePost:listPost){
            FiveZhuang fiveZhuang=new FiveZhuang();
            int fivePostPostId=fivePost.getPostId();
            FiveCommentaries fiveCommentaries=fiveCommentariesMapper.selectByPrimaryId(fivePostPostId);
            Date date = fivePost.getCreateTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String stringDate = format.format(date);
            fivePost.setStringDate(stringDate);
            if(fiveCommentaries!=null) {
                Date date1 = fiveCommentaries.getCommentariesTime();
                SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String stringDate1 = format1.format(date1);
                fiveCommentaries.setStringTime(stringDate1);
            }
            fiveZhuang.setFivePost(fivePost);
            fiveZhuang.setFiveCommentaries(fiveCommentaries);
            list.add(fiveZhuang);
        }
        return list;
    }
    public void save(FivePost fivePost){
        fivePostMapper.insertSelective(fivePost);
    }

    public FiveRcpVo findRcp(int postId){
        FivePost fivePost=fivePostMapper.selectByPrimaryKey(postId);
        Date date = fivePost.getCreateTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String stringDate = format.format(date);
        fivePost.setStringDate(stringDate);
        List<FiveCommentaries> fiveCommentariesList=fiveCommentariesMapper.selectByPrimarycId(postId);
        for(FiveCommentaries fiveCommentaries:fiveCommentariesList){
            Date date1 = fiveCommentaries.getCommentariesTime();
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String stringDate2 = format2.format(date1);
            fiveCommentaries.setStringTime(stringDate2);
        }
//        List<FiveRevert> list=new ArrayList<>();
//        for (FiveCommentaries fiveCommentaries:fiveCommentariesList){
//            int commentariesId= fiveCommentaries.getCommentariesId();
//            List<FiveRevert> fiveReverts=fiveRevertMapper.selectByPrimaryCId(commentariesId);
//            list.addAll(fiveReverts);
//        }
        FiveRcpVo fiveRcpVo=new FiveRcpVo();
        fiveRcpVo.setFivePost(fivePost);
        fiveRcpVo.setFiveCommentaries(fiveCommentariesList);
//        fiveRcpVo.setFiveRevert(list);
        return fiveRcpVo;
    }
    public int findCounta(){
        return fivePostMapper.selectByPrimarypostLabellinga();

    }
    public int findCountb(){
        return fivePostMapper.selectByPrimarypostLabellingb();

    }
    public int findCountc(){
        return fivePostMapper.selectByPrimarypostLabellingc();

    }
    public int findCountd(){
        return fivePostMapper.selectByPrimarypostLabellingd();
    }
    public int findCountasd(){
        return fivePostMapper.findallcount();
    }
    public List<FivePost> findAllpost(int pageNum){
        int firstPage=(pageNum-1)* Constants.PAGE_SIZ;
        return fivePostMapper.selectByPrimaryAll(firstPage,Constants.PAGE_SIZ);
    }


}
